home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c,comp.unix.programmer
- Subject: Re: Interprocess Communication in Linux
- Date: Sat, 27 Jan 96 18:03:48 GMT
- Organization: none
- Distribution: world
- Message-ID: <822765828snz@genesis.demon.co.uk>
- References: <4e768q$sfc@news1.infinet.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4e768q$sfc@news1.infinet.com>
- jmonnin@infinet.com "Joe Monnin" writes:
-
- >I'm having a problem sending a message from one process to another in Linux.
- > My function reads something like:
- >
- >
- >void function{
-
- I assume you meant:
-
- void function(){
-
- > struct msgbuf message;
- >
- > msgid=1;
-
- I must assume 1 is somehow a valid message queue ID
-
- > result=msgsnd(msgid,&message,MSGMAX,IPC_NOWAIT);
- >}
-
- While msgsnd() isn't a part of the C language it is worth looking at the
- language issues in this question. Other discussion should be directed to
- comp.unix.programmer or a Linux newsgroup. Since this is of Unix
- interest I've cross-posed to comp.unix.programmer.
-
- The msgsnd prototype is:
-
- int msgsnd(int msqid, struct msgbuf *msgp, int msgsz, int msgflg);
-
- The problem is that struct msgbuf has a sort of 'struct hack' definition
- which is deemed illegal in ANSI C and results in the whole thing being
- rather messy. If you look in your header files you'll find
- struct msgbuf declared as something like:
-
- struct msgbuf {
- long mtype;
- char mtext[1];
- };
-
- As defined your variable message has room for just 1 'data' byte. So if
- MSGMAX is more than 1 you're telling msgsnd() to use memory outside the
- bounds of your allocated object which is illegal...
-
- >the functions returns a -1 indiacting an error, and errno is set to EFAULT
- > which my documention says means "address
- >pointed to by message is not accessable." Why isn't it accessable? It would
- > be to any other function.
-
- and could easily result in this error. What you need to do in this case is
- define your own structure type as, say:
-
- struct my_msgbuf {
- long mtype;
- char mtext[MSGMAX];
- }
-
- and use:
-
- void function(void)
- {
- int msgid, result;
- struct my_msgbuf message;
-
- /* Fill message */
-
- msgid=1;
- result=msgsnd(msgid,(struct msgbuf *)&message,MSGMAX,IPC_NOWAIT);
- }
-
- This is an example of a very poorly designed system interface. It could
- have been trivially fixed by making mtype an argument to msgsnd thus
- making the 2nd argument a simple char * or void *, which then allows
- you to pass any type of object through msgsnd/msgrcv.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-